//
//  FunctionItemCell.m
//  iOS实战
//
//  Created by clz on 2019/10/26.
//  Copyright © 2019 clz. All rights reserved.
//

#import "FunctionItemCell.h"
#import <Masonry.h>
#import "Commont.h"

@interface FunctionItemCell()

@property(nonatomic,strong)UIImageView *itemImageView;

@property(nonatomic,strong)UILabel *itemTitleLabel;

@end

@implementation FunctionItemCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.functionItemCellVM = [[FunctionItemCellVM alloc]init];
        [self.functionItemCellVM addObserver:self forKeyPath:@"imageName" options:NSKeyValueObservingOptionNew context:nil];
        [self.functionItemCellVM addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];

        
        self.itemImageView = [[UIImageView alloc]init];
        self.itemImageView.image = [UIImage imageNamed:@"home_lesson"];
        [self.contentView addSubview:self.itemImageView];
        [self.itemImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(44, 44));
            make.top.equalTo(self.contentView).offset(0);
            make.centerX.equalTo(self.contentView);
        }];
        
        self.itemTitleLabel = [[UILabel alloc]init];
        self.itemTitleLabel.text = @"测试";
        self.itemTitleLabel.textColor = RGBHex(0X263238);
        self.itemTitleLabel.font = [UIFont systemFontOfSize:14];
        self.itemTitleLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.itemTitleLabel];
        [self.itemTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(self.itemImageView.mas_bottom).offset(15.5 - 3);
            make.bottom.equalTo(self.contentView).offset(-17);
        }];
        
    }
    return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    id value = change[NSKeyValueChangeNewKey];
    
    if ([keyPath isEqualToString:@"imageName"]) {
        
        NSString *imageName = value;
        self.itemImageView.image = [UIImage imageNamed:imageName];
        
    }else if ([keyPath isEqualToString:@"title"]){
        NSString *title = value;
        self.itemTitleLabel.text = title;
    }
}

- (void)dealloc{
    [self.functionItemCellVM removeObserver:self forKeyPath:@"imageName"];
    [self.functionItemCellVM removeObserver:self forKeyPath:@"title"];

}

@end
